//
//  MyWorkVC.m
//  doubleEduStudents
//
//  Created by 盛洋 on 2021/1/12.
//  Copyright © 2021 shengYang. All rights reserved.
//

#import "MyWorkVC.h"
#import "myWorkCollectionCell.h"
#import "signInforCollectionCell2.h"
#import "StudyReportModel.h"

@interface MyWorkVC ()<UICollectionViewDelegate,UICollectionViewDataSource>

//@property (strong, nonatomic) IBOutlet SYButton *left_btn;
//
//@property (strong, nonatomic) IBOutlet SYButton *right_btn;

@property (strong, nonatomic) IBOutlet UICollectionView *collection_view;
@property (nonatomic, strong) NSMutableArray *imgArr;//图片数组
@property (nonatomic, strong)NSDictionary *video;
@end

@implementation MyWorkVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"作品详情";
    [self.backKuang setHidden:YES];
    [self showBackCenterKuang:[UIImage imageNamed:@"back_center"]];
    [self.collection_view registerNib:[UINib nibWithNibName:@"myWorkCollectionCell" bundle:nil] forCellWithReuseIdentifier:@"myWorkCollectionCell"];
    [self.collection_view registerNib:[UINib nibWithNibName:@"signInforCollectionCell2" bundle:nil] forCellWithReuseIdentifier:@"signInforCollectionCell2"];
    self.collection_view.delegate = self;
    self.collection_view.dataSource = self;
    self.collection_view.showsVerticalScrollIndicator = NO;//隐藏竖滚动条
    self.collection_view.showsHorizontalScrollIndicator = NO;
    self.collection_view.pagingEnabled = NO;
    NSLog(@"--%@--",self.valueDic);
    _imgArr = [NSMutableArray array];
    if ([self.valueDic[@"production_pic"] count] > 0) {
        NSArray *arr = [ImgModel mj_objectArrayWithKeyValuesArray:self.valueDic[@"production_pic"]];
        if (arr.count > 0) {
            for (int i = 0; i<arr.count; i++) {
                ImgModel *model = arr[i];
                if ([model.fileUrl containsString:@".jpg"]) {
                    [_imgArr addObject:model.fileUrl];
                }
            }
            
            for (int j = 0; j<arr.count; j++) {
                ImgModel *model = arr[j];
                if ([model.fileUrl containsString:@".mp4"]) {
                    _video = [NSDictionary dictionaryWithObject:model.fileUrl forKey:@"video"];
                }
            }
        }
    }
}

#pragma mark ----- collectionViewDataSource
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 2;
}

//每个item的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return CGSizeMake(self.collection_view.frame.size.width /2, self.collection_view.frame.size.height * 0.9);
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        myWorkCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"myWorkCollectionCell" forIndexPath:indexPath];
        cell.valueDic = self.valueDic;
        return cell;
    }else{
        signInforCollectionCell2 *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"signInforCollectionCell2" forIndexPath:indexPath];
        if (self.imgArr.count > 0) {
            cell.imgArr = self.imgArr;            
        }
        //步骤1：获取视频路径
        NSString *webVideoPath = [NSString stringWithFormat:@"%@",_video[@"video"]];
        NSURL *webVideoUrl = [NSURL URLWithString:webVideoPath];
        //步骤2：创建AVPlayer
        AVPlayer *avPlayer = [[AVPlayer alloc] initWithURL:webVideoUrl];
        //步骤3：使用AVPlayer创建AVPlayerViewController，并跳转播放界面
        AVPlayerViewController *avPlayerVC =[[AVPlayerViewController alloc] init];
        avPlayerVC.player = avPlayer;
        avPlayerVC.videoGravity = AVLayerVideoGravityResize;
        //步骤4：设置播放器视图大小
        avPlayerVC.view.frame = cell.back_view.bounds;
        //特别注意:AVPlayerViewController不能作为局部变量被释放，否则无法播放成功
        //解决1.AVPlayerViewController作为属性
        //解决2:使用addChildViewController，AVPlayerViewController作为子视图控制器
        [self addChildViewController:avPlayerVC];
        [cell.back_view addSubview:avPlayerVC.view];
        return cell;
    }
}

//列间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 20;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
